import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:keyboard_actions/keyboard_actions.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:x_core/util/toast_utils.dart';

/// Created by XieXin on 2020/3/26.
/// 公共工具类
class Utils {
  /// 拨号
  static void launchTelURL(String phone) async {
    final String url = 'tel:$phone';
    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url));
    } else {
      ToastUtils.showLong("拨号失败！");
    }
  }

  ///获取键盘配置信息
  static KeyboardActionsConfig getKeyboardActionsConfig(
      BuildContext context, List<FocusNode> list) {
    return KeyboardActionsConfig(
      nextFocus: true,
      actions: List.generate(
          list.length,
          (i) => KeyboardActionsItem(
                focusNode: list[i],
                toolbarButtons: [
                  (node) {
                    return GestureDetector(
                      onTap: () => node.unfocus(),
                      child: const Padding(
                        padding: EdgeInsets.only(right: 16.0),
                        child: Text("关闭"),
                      ),
                    );
                  }
                ],
              )),
    );
  }

  ///价格格式
  static String formatPrice(String price) {
    return MoneyUtil.withUnit(price, MoneyUnit.YUAN);
  }

  ///价格格式格式化保留两位小数
  ///isSpace 是否添加空格
  static String formatPriceAsFixed2(String price, {bool isSpace = false}) {
    return isSpace
        ? "￥ ${MoneyUtil.changeYWithUnit(
            price,
            MoneyUnit.NORMAL,
            format: MoneyFormat.NORMAL,
          )}"
        : "￥${MoneyUtil.changeYWithUnit(
            price,
            MoneyUnit.NORMAL,
            format: MoneyFormat.NORMAL,
          )}";
  }

  ///文字空指针处理为 无
  static String textHandle(String? text) {
    return TextUtil.isEmpty(text) ? "无" : (text! == "null" ? "无" : text);
  }
}
